package com.greenant.store.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.greenant.store.AntHomeApplication;
import com.greenant.store.R;
import com.greenant.store.bean.AntGoods;
import com.greenant.store.bean.AntGoodsBase;
import com.nostra13.universalimageloader.core.ImageLoader;

public class GoodsBaseListAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private DecimalFormat mDecimalFormat;
	
	private List<AntGoodsBase> mDataList;
	private List<AntGoods> mGoodsFromLibList;
	
	public GoodsBaseListAdapter(Context context, List<AntGoodsBase> dataList, List<AntGoods> goodsFromLibList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(mContext);
		mDataList = dataList;
		mGoodsFromLibList = goodsFromLibList;
		mDecimalFormat = new DecimalFormat("#########.##");
	}

	@Override
	public int getCount() {
		if(mDataList != null) {
			return mDataList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View child = convertView;
		ViewHolder holder = null;
		if(child == null) {
			child = mLayoutInflater.inflate(R.layout.goods_base_list_item, null);
			holder = new ViewHolder(child);
			child.setTag(holder);
		}
		else {
			holder = (ViewHolder)child.getTag();
		}
		
		AntGoodsBase goods = mDataList.get(position);
		ImageLoader.getInstance().displayImage(goods.getImageUrl(), holder.getIvPic(),AntHomeApplication.imageDisplayOptions);
		holder.getTvDes().setText(goods.getDescription());
		holder.getTvUnit().setText(goods.getSpecsDisStr());
		holder.getShowTag().setVisibility(View.INVISIBLE);
		holder.getShowTagSoldOut().setVisibility(View.GONE);
		
		for(AntGoods antGoods : mGoodsFromLibList) {
			if(goods.getObjectId().equals(antGoods.getSrcId())) {
				holder.getShowTag().setVisibility(View.VISIBLE);
				break;
			}
		}
		
		return child;
	}
	
	private class ViewHolder {
		private View mContentView;
		private ImageView mIvPic;
		private View mShowTag;
		private View mShowTagSoldOut;
		private TextView mTvDes;
		private TextView mTvPrice;
		private TextView mTvUnit;
        
		public ViewHolder(View contentView) {
			mContentView = contentView;
		}
		
		public View getShowTag() {
			if(mShowTag == null) {
				mShowTag = mContentView.findViewById(R.id.goods_list_item_pic_show_tag);
			}
			return mShowTag;
		}
		
		public View getShowTagSoldOut() {
			if(mShowTagSoldOut == null) {
				mShowTagSoldOut = mContentView.findViewById(R.id.goods_list_item_pic_show_tag_sold_out);
			}
			return mShowTagSoldOut;
		}

		public ImageView getIvPic() {
			if(mIvPic == null) {
				mIvPic = (ImageView)mContentView.findViewById(R.id.goods_list_item_pic);
			}
			return mIvPic;
		}

		public TextView getTvDes() {
			if(mTvDes == null) {
				mTvDes = (TextView)mContentView.findViewById(R.id.goods_list_item_des);
			}
			return mTvDes;
		}

		public TextView getTvPrice() {
			if(mTvPrice == null) {
				mTvPrice = (TextView)mContentView.findViewById(R.id.goods_list_item_price);
			}
			return mTvPrice;
		}

		public TextView getTvUnit() {
			if(mTvUnit == null) {
				mTvUnit = (TextView)mContentView.findViewById(R.id.goods_list_item_unit);
			}
			return mTvUnit;
		}
		
	}

}








